import {INPUT_FOCUS, INPUT_BLUR, CHANGE_LIST, MOUSE_ENTER, MOUSE_LEAVE, CHANGE_PAGE} from './constants';
//immutable提供了一个fromJS的方法，可以帮助我们把一个js对象转化为immutable对象
import {fromJS} from "immutable";
import {bindReporter} from "web-vitals/dist/modules/lib/bindReporter";
//1. 第一步将数据转换成 immutable对象
const defaultState = fromJS({
    focused: false,
    mouseIn: false,
    list: [],
    page: 1, //当前页码
    totalPage: 1, //整体的页数
})
const reducer = (state = defaultState, action) => {
    // const newState = JSON.parse(JSON.stringify(state));
    // switch (action.type) {
    //     case INPUT_FOCUS:
    //         newState.focused = true;
    //         break
    //     case INPUT_BLUR:
    //         newState.focused = false
    //         break
    //     default:
    //         break;
    // }
    // return newState;

    switch (action.type) {
        case INPUT_FOCUS:
            //immutable对象的set方法，会结合之前immutable对象的值和设置的值，返回一个全新的对象
            return state.set('focused', true);
        case INPUT_BLUR:
            return state.set('focused', false);
        case CHANGE_LIST:
            return state.merge({
                list: action.data,
                totalPage: action.totalPage
            })
        // return state.set('list', action.data).set('totalPage', action.totalPage);
        case MOUSE_ENTER:
            return state.set('mouseIn', true);
        case MOUSE_LEAVE:
            return state.set('mouseIn', false);
        case CHANGE_PAGE:
            return state.set('page', action.page);
        default:
            return state;
    }
};
export default reducer;